﻿using System;
using System.IO;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
using AudioPlayer.Plugin.Storage;
namespace AudioPlayer.Database {
	public class Library {
		#region Variables
		private IPlaylist _active;
		#endregion
		#region Constructor
		public Library() {
			InitLibrary();
		}
		#endregion
		#region Private Voids
		private void InitLibrary() {
			//Make sure we have storage
			if (Config.Plugins.HasStorage) {
				
				Config.Plugins.Storage.OnUpdate += new EventHandler(Storage_Update);
				Config.Plugins.Storage.Read();
				_active = this.Collection;
			}
		}
		private void ScanDir(string dir) {
			if (Config.Plugins.Watcher != null) {
				Config.Plugins.Watcher.Update += new EventHandler(Watcher_Update);
				Config.Plugins.Watcher.Scan(dir, Config.Plugins.TagProvider.Formats);
			}
		}
		private void Scan(string file, bool raiseUpdate) {
			if (Directory.Exists(file)) {
				ScanDir(file);
			} else {
				if (File.Exists(file)) {
					if (Config.Plugins.TagProvider != null) {
						ISong s = Config.Plugins.TagProvider.Read(file);
						if (s != null && s.Valid) {

							this.Collection.Add(s);
							if (this.Active != this.Collection) {
								this.Active.Add(s);
							}
							if (raiseUpdate){
								this.OnUpdate();
							}
						}
					}
				}
			}
		}
		#endregion
		#region Public Voids
		public void Scan(string file) {
			this.Scan(file, true);
		}
		public void Scan(string[] files) {
			foreach (string s in files) {
				this.Scan(s,false);
			}
			this.OnUpdate();
		}
		public void Scan(IList<string> files) {
			foreach (string s in files) {
				this.Scan(s,false);
			}
			this.OnUpdate();
		}

		public void Activate(IPlaylist pl) {
			if (pl != null) {
				if (!pl.Loaded) {
					Config.Plugins.Storage.Load(pl);
				}
			}
			this.Active = pl;
		}
		#endregion
		#region Property
		public IPlaylist Collection {
			get { return Config.Plugins.Storage.Library; }
			//set { _collection = value; }
		}
		public IPlaylist Active {
			get { return _active; }
			set { 
				_active = value;
				this.OnUpdate();
			}
		}
		public IList<IPlaylist> Playlists {
			get { return Config.Plugins.Storage.Playlists; }
			set { 
				//_playLists = value;
				this.OnPlaylistChanged();
			}
		}
		#endregion
		#region Events
		public event EventHandler Update;
		public event EventHandler PlaylistChanged;

		protected virtual void OnUpdate() {
			if (this.Update != null) {
				this.Update(this, EventArgs.Empty);
			}
		}
		protected virtual void OnPlaylistChanged() {
			if (PlaylistChanged != null) {
				PlaylistChanged(this, EventArgs.Empty);
			}
		}
		#endregion
		#region Watcher
		private void Watcher_Update(object sender, EventArgs e) {
			IList<String> files = Config.Plugins.Watcher.GetFiles();
			//loop through
			foreach (string path in files) {
				//create a song
				ISong s = Config.Plugins.TagProvider.Read(path);
				//check if it's ok, and we have storage
				if (s != null && Config.Plugins.HasStorage) {
					//Add to main collection.
					Config.Plugins.Storage.Put(this.Collection, s);
					//Let's check if user isn't using custom playlist
					if (this.Collection != this.Active) {
						//If he is, add to his playlist.
						Config.Plugins.Storage.Put(this.Active, s);
					}
				}
			}
			//disconnect
			Config.Plugins.Watcher.Update -= Watcher_Update;
			//Notify list control
			this.OnUpdate();
		}
		#endregion
		#region Storage
		private void Storage_Update(object sender, EventArgs e) {

		}
		#endregion
	}
}